<?php



namespace app\admin\controller;

use app\common\library\enum\CodeEnum;
use app\common\library\enum\UserStatusEnum;

class User extends BaseAdmin
{

    /**
     * 商户
     *
     *
     * @return mixed
     */
    public function index()
    {

        return $this->fetch();
    }

    /**
     * 商户列表
     *
     *
     */
    public function getList()
    {
        $where = [];

        //组合搜索
        !empty($this->request->param('uid')) && $where['a.uid']
            = ['eq', $this->request->param('uid')];

        !empty($this->request->param('email')) && $where['a.account']
            = ['like', '%' . $this->request->param('email') . '%'];

        is_numeric($this->request->param('status')) && $where['a.status']
            = ['eq', $this->request->param('status')];

        $data = $this->logicUser->getUserList($where, 'a.create_time desc', $this->request->param('limit'));

        $this->result(
            $data && !empty($data) && $data->total() > 0 ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $data->total(),
                    'data' => $data->items()
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => 0,
                    'data' => []
                ]
        );
    }


    /**
     * 添加商户
     *
     *
     * @return mixed
     */
    public function add()
    {
        // post 是提交数据
        $this->request->isPost() && $this->result($this->logicUser->addUser($this->request->post()));

        return $this->fetch();
    }

    /**
     * 编辑商户基本信息
     *
     *
     * @return mixed
     */
    public function edit()
    {
        // post 是提交数据
        $this->request->isPost() && $this->result($this->logicUser->editUser($this->request->post()));
        //获取商户详细信息
        $this->assign('user', $this->logicUser->getUserInfo(['uid' => $this->request->param('id')]));

        return $this->fetch();
    }

    public function cash()
    {
        // post 是提交数据
        $this->request->isPost() && $this->result($this->logicUser->editUserCash($this->request->post()));
        //获取商户详细信息
        $this->assign('user', $this->logicUser->getUserInfo(['uid' => $this->request->param('id')]));
        
        $this->assign('channels', $this->logicPay->getChannelList([], true, 'create_time desc',false));

        return $this->fetch();
    }

    /**
     * 修改商户状态
     *
     *
     * @param int $id
     * @param bool $status
     */
    public function changeStatus($id = 0, $status = false)
    {

        $this->result($this->logicUser->setUserStatus(['uid' => $id], $status == 1 ? '0' : '1'));
    }

    /**
     * 删除商户
     *
     *
     */
    public function del()
    {
        // post 是提交数据
        $this->request->isPost() && $this->result(
            $this->logicUser->delUser(
                [
                    'uid' => $this->request->param('uid')
                ]
            )
        );
        // get 直接报错
        $this->error([CodeEnum::ERROR, '未知错误']);
    }

    /**
     * 认证信息
     *
     *
     * @return mixed
     */
    public function auth()
    {
        return $this->fetch();
    }

    /**
     * 商户认证信息列表
     *
     *
     */
    public function getAuthList()
    {
        $where = [];

        //组合搜索
        !empty($this->request->param('uid')) && $where['uid']
            = ['eq', $this->request->param('uid')];

        is_numeric($this->request->param('status')) && $where['status']
            = ['eq', $this->request->param('status')];

        //时间搜索  时间戳搜素
        $where['create_time'] = $this->parseRequestDate();

        $data = $this->logicUser->getUserAuthList($where, true, 'create_time desc', $this->request->param('limit'));

        $this->result(
            $data && !empty($data) && $data->total() > 0 ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $data->total(),
                    'data' => $data->items()
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => 0,
                    'data' => []
                ]
        );
    }

    /**
     * 认证详细信息
     *
     *
     * @return mixed
     */
    public function userAuthInfo()
    {
        // post 是提交数据
        $this->request->isPost() && $this->result($this->logicUser->saveUserAuth($this->request->post()));
        //获取认证详细信息
        $auth = $this->logicUser->getUserAuthInfo(['uid' => $this->request->param('id')]);
        $auth['card'] = json_decode($auth['card'], true);

        $this->assign('auth', $auth);

        return $this->fetch();
    }

    /**
     * 分润设置
     *
     *
     *
     * @return mixed
     */
    public function profit()
    {
        // post 是提交数据
        if ($this->request->isPost()) {
            $data = $this->request->post('r/a');
            foreach ($data as $key => $item) {
                //查
                $profit = $this->logicUser->getUserProfitInfo(['uid' => $item['uid'], 'cnl_id' => $item['cnl_id']]);
                if ($profit) {
                    $data_update[] = [
                        'id' => $profit['id'],
                        'uid' => $item['uid'],
                        'cnl_id' => $item['cnl_id'],
                        'urate' => $item['urate'],
                        'grate' => 0, //$item['grate'],
                        'status' => 1
                    ];
                } else {
                    $data_update[] = [
                        'uid' => $item['uid'],
                        'cnl_id' => $item['cnl_id'],
                        'urate' => $item['urate'],
                        'grate' => 0, //$item['grate'],
                        'status' => 1
                    ];
                }
            }
            $this->result($this->logicUser->saveUserProfit($data_update));
        };
        //所有渠道列表
        $channel = $this->logicPay->getChannelList([], true, 'create_time desc', false);

        //获取商户分润详细信息
        $userProfit = $this->logicUser->getUserProfitList(['uid' => $this->request->param('id')]);
        if ($userProfit) {
            foreach ($userProfit as $item) {
                $_tmpData[$item['cnl_id']] = $item;
            }
        }

        //重组渠道列表
        if ($channel) {
            foreach ($channel as $key => $item) {
                //dump($item);
                $channel[$key]['urate'] = isset($_tmpData[$item['id']]['urate']) ? $_tmpData[$item['id']]['urate'] : $item['urate'];
                $channel[$key]['grate'] = isset($_tmpData[$item['id']]['grate']) ? $_tmpData[$item['id']]['grate'] : $item['grate'];
            }
        }
        $this->assign('list', $channel);;

        return $this->fetch();
    }
}
